package com.carlwang.action;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

public class HibernateUtil {

	private SessionFactory mSessionFactory;
	private ThreadLocal<Session> session = new ThreadLocal<Session>();

	public SessionFactory getSessionFactory() {
		return mSessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		mSessionFactory = sessionFactory;
	}

	public Session getSession() {
		Session session = this.session.get();
		if (session == null) {
			session = getSessionFactory().openSession();
		}
		return session;
	}

	public void closeSession() {
		Session session = this.session.get();
		if (session != null) {
			session.close();

		}
		this.session.set(null);
	}
}
